package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.entity.ShoppingCart;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.service.ShoppingCartService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @param :
 * @description: 购物车实现类模块
 * @author: Acer
 * @date: 2022/6/8
 * @return:
 */

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 获取当前用户的购物车列表数据
     *
     * @return
     */
    @Override
    public List<ShoppingCart> shoppingList() {
        //定义shoppingCarList初试为空Null
        List<ShoppingCart> shoppingCartList = null;
        //获取当前用户id
        Long userId = BaseContext.getUserId();
        //如果数据库查不到用户id，则购物车为空
        if (userId == null) {
            return shoppingCartList;
        }
        //返回查询的条件结果
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //拿得到的用户id与数据库的用户做精准对比
        wrapper.eq(ShoppingCart::getUserId, userId);
        //新加的商品排在前面,根据时间排序
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        shoppingCartList = shoppingCartMapper.selectList(wrapper);
        return shoppingCartList;
    }

    /**
     * 购物车添加商品
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart shoppingAdd(ShoppingCart shoppingCart) {
        ShoppingCart selectOne = null;
        if (shoppingCart == null) {
            return null;
        }
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        if (setmealId == null && dishId == null) {
            return null;
        }
        //获取当前登录用户id
        Long userId = BaseContext.getUserId();
        //根据用户id userId  菜品id dishId 套餐id  setmealId dishFlavor
        //查询购物车中菜品是否已经存在，存在则修改数量（number+1）,不存在则新增数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //添加 userid条件-查询当前用户购物车
        wrapper.eq(ShoppingCart::getUserId, userId);
        // 菜品的话，添加菜品条件
        if (dishId != null) {
            wrapper.eq(ShoppingCart::getDishId, dishId);
            //菜品 需要添加口味
            String dishFlavor = shoppingCart.getDishFlavor();
            if (StringUtils.isNotBlank(dishFlavor)) {
                wrapper.eq(ShoppingCart::getDishFlavor, dishFlavor);
            } else {
                shoppingCart.setDishFlavor("");
                wrapper.eq(ShoppingCart::getDishFlavor, "");
            }
        }
        //套餐的话，添加套餐条件
        if (setmealId != null) {
            wrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        selectOne = shoppingCartMapper.selectOne(wrapper);
        //如果库中该菜品或者套餐已经存在，修改菜品套餐数量就可以
        if (selectOne != null) {
            selectOne.setNumber(selectOne.getNumber() + 1);
            shoppingCartMapper.updateById(selectOne);
        } else {
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            selectOne = shoppingCart;
            shoppingCartMapper.insert(shoppingCart);
        }
        return selectOne;
    }

    /**
     * 功能描述 :删除购物车中菜品或者套餐
     *
     * @param shoppingCart
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public ShoppingCart shoppingDel(ShoppingCart shoppingCart) {
        ShoppingCart selectOne = null;
        int row = 0;
        if (shoppingCart == null) {
            return selectOne;
        }
        Long setmealId = shoppingCart.getSetmealId();
        Long dishId = shoppingCart.getDishId();
        if (dishId == null && setmealId == null) {
            return selectOne;
        }
        Long userId = BaseContext.getUserId();

        if (userId == null) {
            return selectOne;
        }
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();

        //添加userId条件-----查询当前用户购物车
        wrapper.eq(ShoppingCart::getUserId, userId);

        if (dishId != null) {
            wrapper.eq(ShoppingCart::getDishId, dishId);
        }

        if (setmealId != null) {
            wrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }

        Long id = shoppingCart.getId();
        wrapper.eq(id != null, ShoppingCart::getId, id);
        selectOne = shoppingCartMapper.selectOne(wrapper);
        Integer number = selectOne.getNumber();
        number = number - 1;
        if (number == 0) {
            shoppingCartMapper.deleteById(selectOne);
        }
        selectOne.setNumber(number);
        shoppingCartMapper.update(selectOne, wrapper);
        return selectOne;

    }

    /**
     * 根据用户id清空购物车
     *
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int shoppingDelAll() {
        int row = 0;
        Long userId = BaseContext.getUserId();
        if (userId == null) {
            return row;
        }
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        row = shoppingCartMapper.delete(wrapper);
        return row;
    }


}

